#include "mpHttpClientAsyncImpl.h"
#include "mpHttpDispatcher.h"
#include "mpLog.h"

namespace MP{
namespace HTTP{
namespace INTERNAL{

HttpClientAsyncImpl::HttpClientAsyncImpl(boost::asio::io_context& io_context)
    : HttpClientAsync(),_io_context(io_context)
{
    _httpConnectionManage = createHttpConnectionManage(_io_context);
}

HttpClientAsyncImpl::~HttpClientAsyncImpl(){
    //关闭所有请求，如果有正在运行的HTTP请求将报出HTTP连接异常
    _httpConnectionManage->clearAndDeactivate();
}

void HttpClientAsyncImpl::cancel(){
    //取消所有正在运行的HTTP请求，程序将报出HTTP连接异常
    _httpConnectionManage->closeIsRunning();
}

void HttpClientAsyncImpl::sendAsync(HttpRequestMessage::ptr_s httpRequest,
                                    HttpResponseMessage::ptr_s httpResponse,
                                    func_send_complete_callback&& complete_callback,
                                    func_send_progress_callback&& progress_callback){

    std::shared_ptr<HttpDispatcherAsync> dispatcherAsync(std::make_shared<HttpDispatcherAsync>(_io_context,
                                                                                               _httpConnectionManage,
                                                                                               httpRequest,
                                                                                               httpResponse,
                                                                                               get_connect_timeout(),
                                                                                               get_write_timeout(),
                                                                                               get_read_timeout()));
    if(progress_callback){
        dispatcherAsync->set_send_progress_callback(std::move(progress_callback));
    }

    if(complete_callback){
        dispatcherAsync->set_send_complete_callback(std::move(complete_callback));
    }

    dispatcherAsync->execute_request_async();
}

//head
void HttpClientAsyncImpl::headAsync(const std::string& relativeUri,
                                    func_send_complete_callback&& complete_callback,
                                    func_send_progress_callback&& progress_callback)
{
    //创建Req
    Url url(_baseAddress,relativeUri);
    HttpRequestMessage::ptr_s httpRequest = createHttpRequestMessage(url,MP::HTTP::HTTP_METHOD::HTTP_HEAD);
    initHttpRequestMessageHeaders(httpRequest);

    HttpRequestContent::ptr_s httpRequestContent = createHttpRequestEmptyContent();
    httpRequest->setHttpRequestContent(httpRequestContent);

    //创建Rep
    HttpResponseMessage::ptr_s httpResponse = createHttpResponseMessage();
    //发送请求
    sendAsync(httpRequest,httpResponse,std::move(complete_callback),std::move(progress_callback));
}


//get
void HttpClientAsyncImpl::getAsync(const std::string& relativeUri,
                                   func_send_complete_callback&& complete_callback,
                                   func_send_progress_callback&& progress_callback){

    //创建Req
    Url url(_baseAddress,relativeUri);
    HttpRequestMessage::ptr_s httpRequest = createHttpRequestMessage(url,MP::HTTP::HTTP_METHOD::HTTP_GET);
    initHttpRequestMessageHeaders(httpRequest);

    HttpRequestContent::ptr_s httpRequestContent = createHttpRequestEmptyContent();
    httpRequest->setHttpRequestContent(httpRequestContent);
    //创建Rep
    HttpResponseMessage::ptr_s httpResponse = createHttpResponseMessage();
    //发送请求
    sendAsync(httpRequest,httpResponse,std::move(complete_callback),std::move(progress_callback));
}

void HttpClientAsyncImpl::downloadAsync(const std::string& relativeUri,
                                        const std::string& filePath,
                                        std::size_t fileOffset,
                                        func_send_complete_callback&& complete_callback,
                                        func_send_progress_callback&& progress_callback) {
    //创建Req
    Url url(_baseAddress,relativeUri);
    HttpRequestMessage::ptr_s httpRequest = createHttpRequestMessage(url,MP::HTTP::HTTP_METHOD::HTTP_GET);
    initHttpRequestMessageHeaders(httpRequest);

    HttpRequestContent::ptr_s httpRequestContent = createHttpRequestEmptyContent();
    httpRequest->setHttpRequestContent(httpRequestContent);
    //创建Rep
    HttpResponseMessage::ptr_s httpResponse = createHttpResponseFileMessage(filePath,fileOffset);
    //发送请求
    sendAsync(httpRequest,httpResponse,std::move(complete_callback),std::move(progress_callback));
}

//post
void HttpClientAsyncImpl::postAsync(const std::string& relativeUri,
                                    HttpRequestContent::ptr_s content,
                                    func_send_complete_callback&& complete_callback,
                                    func_send_progress_callback&& progress_callback) {
    //创建Req
    Url url(_baseAddress,relativeUri);
    HttpRequestMessage::ptr_s httpRequest = createHttpRequestMessage(url,MP::HTTP::HTTP_METHOD::HTTP_POST);
    initHttpRequestMessageHeaders(httpRequest);

    httpRequest->setHttpRequestContent(content);
    //创建Rep
    HttpResponseMessage::ptr_s httpResponse = createHttpResponseMessage();
    //发送请求
    sendAsync(httpRequest,httpResponse,std::move(complete_callback),std::move(progress_callback));

}

//put
void HttpClientAsyncImpl::putAsync(const std::string& relativeUri,
                                   HttpRequestContent::ptr_s content,
                                   func_send_complete_callback&& complete_callback,
                                   func_send_progress_callback&& progress_callback) {
    //创建Req
    Url url(_baseAddress,relativeUri);
    HttpRequestMessage::ptr_s httpRequest = createHttpRequestMessage(url,MP::HTTP::HTTP_METHOD::HTTP_PUT);
    initHttpRequestMessageHeaders(httpRequest);

    httpRequest->setHttpRequestContent(content);
    //创建Rep
    HttpResponseMessage::ptr_s httpResponse = createHttpResponseMessage();
    //发送请求
    sendAsync(httpRequest,httpResponse,std::move(complete_callback),std::move(progress_callback));
}

//delete
void HttpClientAsyncImpl::delAsync(const std::string& relativeUri,
                                   func_send_complete_callback&& complete_callback,
                                   func_send_progress_callback&& progress_callback) {
    //创建Req
    Url url(_baseAddress,relativeUri);
    HttpRequestMessage::ptr_s httpRequest = createHttpRequestMessage(url,MP::HTTP::HTTP_METHOD::HTTP_DELETE);
    initHttpRequestMessageHeaders(httpRequest);

    HttpRequestContent::ptr_s httpRequestContent = createHttpRequestEmptyContent();
    httpRequest->setHttpRequestContent(httpRequestContent);
    //创建Rep
    HttpResponseMessage::ptr_s httpResponse = createHttpResponseMessage();
    //发送请求
    sendAsync(httpRequest,httpResponse,std::move(complete_callback),std::move(progress_callback));
}



}   //INTERNAL
}   //HTTP
}   //MP


